#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
__author__ = 'Administrator'

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle(self.tr("打印图片"))
        self.imageLabel = QLabel()
        self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.setCentralWidget(self.imageLabel)

        self.image = QImage()
        self.html=u'<html><head></head><body><h1>55555</h1><b>bold</b></body></html>'

        self.createActions()
        self.createMenus()
        self.createToolBars()

        if self.image.load("search.png"):
            self.imageLabel.setPixmap(QPixmap.fromImage(self.image))
            self.resize(self.image.width(), self.image.height())

    def createActions(self):
        self.PrintAction = QAction(QIcon("images/print.png"), self.tr("打印"), self)
        self.PrintAction.setShortcut("Ctrl+P")
        self.PrintAction.setStatusTip(self.tr("打印"))
        self.connect(self.PrintAction, SIGNAL("triggered()"), self.slotPrint)

    def createMenus(self):
        PrintMenu = self.menuBar().addMenu(self.tr("打印"))
        PrintMenu.addAction(self.PrintAction)

    def createToolBars(self):
        fileToolBar = self.addToolBar("Print")
        fileToolBar.addAction(self.PrintAction)

    def slotPrint(self):
        printer = QPrinter()
        printDialog = QPrintDialog(printer, self)
        if printDialog.exec_():
            self.painter = QPainter(printer)
            rect = self.painter.viewport()
            size = self.image.size()
            size.scale(rect.size(), Qt.KeepAspectRatio)
            self.painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
            # self.painter.setWindow(self.image.rect())
            self.painter.drawImage(0, 0, self.image)
            self.painter.drawText(0,0,self.html)


app = QApplication(sys.argv)
main = MainWindow()
main.show()
app.exec_()
